package com.gaolu;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp2.BasicDataSource;

public class DBCPDataSourceFactory {
	public static DataSource getDataSource(String dbType){
		Properties props = new Properties();
		InputStream is = DBCPDataSourceFactory.class.getClassLoader().getResourceAsStream("db.properties");
		try {
			props.load(is);
		} catch (IOException e) {
			e.printStackTrace();
		}
		BasicDataSource ds = new BasicDataSource();
		if("mysql".equals(dbType)){
			ds.setDriverClassName(props.getProperty("MYSQL_DB_DRIVER_CLASS"));
			ds.setUrl(props.getProperty("MYSQL_DB_URL"));
			ds.setUsername(props.getProperty("MYSQL_DB_USERNAME"));
			ds.setPassword(props.getProperty("MYSQL_DB_PASSWORD"));
			return ds;
		}
		return null;
	}
}
